package com.example.treasureadmin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasureadmin.config.AliOSSConfig;
import com.example.treasureadmin.mapper.MapMapper;
import com.example.treasureadmin.model.MapModel;
import com.example.treasureadmin.model.OrderModel;
import com.example.treasureadmin.util.AliOSSUtils;
import com.example.treasureadmin.util.FileUrlUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class MapService {
    @Autowired
    private MapMapper mapMapper;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private AliOSSConfig aliOSSConfig;
    @Autowired
    private FileUrlUtils fileUrlUtils;
    //获取所有用户提交的站点
    public List<MapModel> getAll()
    {
        QueryWrapper<MapModel> queryWrapper = new QueryWrapper<>();
        return mapMapper.selectList(queryWrapper);
    }
    //添加站点
    public void addMap(String lng,String lat,String image)
    {
        Timestamp t = new Timestamp(System.currentTimeMillis());
        mapMapper.insert(new MapModel(-1,lng,lat,t,image,0));
    }
    //更改站点状态
    public void changeStatus(int id,int status)
    {
        UpdateWrapper<MapModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("status",status);
        if (status == 1){
            MapModel mapModel = mapMapper.selectById(id);
            try {
                updateCSV(mapModel.getLng(),mapModel.getLat());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        mapMapper.update(null,updateWrapper);
    }
    //根据站点状态查找站点
    public List<MapModel> getStatusMap(int status){
        QueryWrapper<MapModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        return mapMapper.selectList(queryWrapper);
    }
    //图片上传
    public String uploadImage(InputStream inputStream, String suffix) {
        String fileName = RandomStringUtils.randomAlphanumeric(32).toUpperCase();
        String mapPath = aliOSSConfig.MAP_PATH + fileName + suffix;
        aliOSSUtils.uploadAvatar(inputStream, mapPath);
        return aliOSSConfig.PATH+mapPath;
    }
    // 修改csv并上传
    public void updateCSV(String lng, String lat) throws FileNotFoundException {
        String localPath = ResourceUtils.getURL("classpath:").getPath() + "temp.csv";
        fileUrlUtils.downloadFile(aliOSSConfig.PATH+aliOSSConfig.TRASH_DATA,localPath);
        File csv = new File(localPath);
        try{
            //第二步：通过FileWriter类创建一个使用默认大小输出缓冲区的缓冲字符输出流
            FileWriter writeText = new FileWriter(csv,true);

            //第三步：将文档的下一行数据赋值给lineData，并判断是否为空，若不为空则输出
            String info = lng + ","+lat;
            info =info +System.getProperty("line.separator");
            writeText.write(info);
            //使用缓冲区的刷新方法将数据刷到目的地中
            writeText.flush();
            //因此，此处的close()方法关闭的是被缓存的流对象
            writeText.close();
        }catch (FileNotFoundException e){
            System.out.println("没有找到指定文件");
        }catch (IOException e){
            System.out.println("文件读写出错");
        }
        aliOSSUtils.uploadAvatar(new FileInputStream(csv), aliOSSConfig.TRASH_DATA);
    }
}
